package com.getguang.superstore.search

import android.graphics.Rect
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.core.view.isVisible
import androidx.core.view.postDelayed
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.KeyboardUtils
import com.getguang.superstore.cell.DealsItemCell
import com.getguang.superstore.cell.toLightMode
import com.getguang.superstore.databinding.ActivitySsdSearchBinding
import com.getguang.superstore.databinding.CellDealsItemBinding
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.invisible
import com.starblink.android.basic.ext.justLoadMore
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.widget.recycler.BottomLineItemDecor.Companion.noBottomLine
import com.starblink.android.basic.widget.recycler.BottomLineItemDecor.Companion.noMoreData
import com.starblink.android.basic.widget.recycler.BottomLineItemDecor.Companion.refresh
import com.starblink.basic.route.RoutePage


/**
 * Super Deals Search Page
 *  Create by yuanl at 2023/10/10
 **/
@Route(path = RoutePage.SuperStoreDeal.SEARCH)
class SSDealsSearchActivity : BaseTVMActivity<ActivitySsdSearchBinding, SsdSearchVM>() {
    override fun initViewBinding(): ActivitySsdSearchBinding =
        ActivitySsdSearchBinding.inflate(layoutInflater)

    override fun isStatusBarLightMode(): Boolean = false

    @JvmField
    @Autowired(name = "deals")
    var deals: Array<String>? = null

    private val adapter by lazyOnNone {
        MutableAdapter(viewModel.list).apply {
            addVhDelegate {
                DealsItemCell(CellDealsItemBinding.inflate(layoutInflater, it, false).apply {
                    toLightMode()
                })
            }
        }
    }

    override fun initData() {
        super.initData()
        viewModel.deals = deals
    }

    override fun initView() {
        super.initView()
        viewBinding.edSearch.postDelayed(500L) {
            viewBinding.edSearch.requestFocus()
            KeyboardUtils.showSoftInput(viewBinding.edSearch)
        }
        viewBinding.edSearch.onClearListener = {
//            viewModel.list.clear()
//            adapter.refreshAll()
//            viewBinding.tvResultTitle.invisible()
//            viewBinding.layoutResult.invisible()
//            pageDisappear()
        }
        viewBinding.edSearch.setOnEditorActionListener { v, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                viewModel.searchContent = v.text.toString()
                KeyboardUtils.hideSoftInput(v)
                v.clearFocus()
                doSearch()
                true
            } else {
                false
            }
        }
        viewBinding.edSearch.setOnFocusChangeListener { v, hasFocus ->
//            if (hasFocus) {
//                viewBinding.tvResultTitle.invisible()
//                viewBinding.layoutResult.invisible()
//                pageDisappear()
//            }
        }

        viewBinding.recycler.layoutManager = LinearLayoutManager(this)
        viewBinding.recycler.addItemDecoration(object : ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                super.getItemOffsets(outRect, view, parent, state)
                outRect.top = 12.dp
            }
        })
        viewBinding.recycler.adapter = adapter
        viewBinding.refresher.noBottomLine()
        viewBinding.refresher.setEnableAutoLoadMore(true)
        viewBinding.refresher.setOnLoadMoreListener {
            viewModel.loadData(true)
        }
//        viewBinding.refresher.justLoadMore {
//
//        }
    }

    private fun doSearch() {
        if (viewModel.searchContent.isNullOrEmpty()) {
            onBackPressed()
            return
        }
        viewBinding.refresher.setNoMoreData(false)
        viewModel.loadData()
        viewBinding.recycler.refresh()
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.dataObserver.observe(this) {
            viewBinding.layoutPreview.gone()
            viewBinding.tvResultTitle.visible()
            viewBinding.layoutResult.visible()
            if (it) {
                viewBinding.refresher.finishLoadMore()
            } else {
                viewBinding.refresher.finishLoadMoreWithNoMoreData()
                viewBinding.recycler.noMoreData(
                    textColor = color(com.starblink.basic.style.R.color.gu_text_disabled),
                    lineColor = color(com.starblink.basic.style.R.color.gu_neutral_200)
                )
            }

            if (viewModel.pageNo == 1) {
                viewBinding.refresher.visibleOrGone(it)
                viewBinding.empty.visibleOrGone(!it)
                pageExposure()
            }

            adapter.refreshAll()
        }
    }

    override fun spmPageId(): Int = SpmPageDef.PAGE_10240

    override fun spmPageParams(): Map<String, String> = mapOf(
        "searchTerm" to viewModel.searchContent.orEmpty(),
        "searchType" to "normal"
    )

    override fun onResume() {
        super.onResume()
        if (viewBinding.layoutResult.isVisible) {
            pageExposure()
        }
    }

    override fun onPause() {
        super.onPause()
        if (viewBinding.layoutResult.isVisible) {
            pageDisappear()
        }
    }


}